/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import who.tmap.bo.cd.CDColorPattern;
import who.tmap.bo.cd.ColorPatternClassification;
import who.tmap.bo.cd.ColorPatternRange;
import who.tmap.toolkit.impl.AnnotationsHelper;
import who.tmap.toolkit.shape.annotation.LegendShape;

public class ColorPatternLegendShapeHelper  {

	private LegendShape legendShape;
	
	public ColorPatternLegendShapeHelper( LegendShape legendShape ) {
		this.legendShape =legendShape;
	}
	
	public LegendShape getLegendShape() {
		return legendShape;
	}

	public void setLegendShape(LegendShape legendShape) {
		this.legendShape = legendShape;
	}
	
	protected TextRenderingHelper prepareLegend( Graphics2D g, Point2D legendTextPt, double screenToMapFactor ) {
		
		CDColorPattern cdc =(CDColorPattern)legendShape.getCartographicDisplay();
		TextRenderingHelper trh =new TextRenderingHelper( screenToMapFactor, legendShape.getZoomFactor() );
		
		trh.init( g, legendShape.getFontName(), legendShape.getFontStyle(), legendShape.getFontSize() );			
		
		AnnotationsHelper annHelper =new AnnotationsHelper();
		
		NumberFormat formatter =NumberFormat.getNumberInstance();
		
		Map<Integer, ColorPatternRange> ranges =cdc.getRanges();
		if ( ranges != null ) {
			List<String> lines =new ArrayList<String>();
			int rangeCt =0;
			for( ColorPatternRange range : ranges.values() ) {
				
				Map<String, Object> ctx =new HashMap<String, Object>();
				if ( cdc.getClassification() == ColorPatternClassification.NON_NUMERIC ) {
					ctx.put( AnnotationsHelper.COLOR_PATTERN_NON_NUMERIC, range.getCaption() );
				} else {
					ctx.put( AnnotationsHelper.COLOR_PATTERN_LOW_RANGE 
						   , formatter.format( legendShape.isColorPatternRoundRangesBoundaries() ? Double.valueOf( range.getFrom() ).longValue() : range.getFrom() ) );
					ctx.put( AnnotationsHelper.COLOR_PATTERN_HIGH_RANGE
						   , formatter.format( legendShape.isColorPatternRoundRangesBoundaries() ? Double.valueOf( range.getTo() ).longValue() : range.getTo() ) );
				}
				
				// modified: previously String val =legendShape.getColorPatternLegend().get( rangeCt++ );
				String val =rangeCt < legendShape.getColorPatternLegend().size() ?
								 legendShape.getColorPatternLegend().get( rangeCt++ )
								: " ";				
				String line =annHelper.replaceAnnotations( ctx, val );
				
				lines.add( line );
				legendShape.getLegendSamples().add( new LegendSample( new Point2D.Double( legendShape.getPt().getX(), legendTextPt.getY() + trh.getYOffsetForLine( rangeCt-1 ) )
												   , range.getColor()
												   , legendShape.getTexturePaint( range.getFillPattern() )
												   , -1, null ) );
			}
				
			trh.prepareTextForRendering( g, lines, legendTextPt, TextRenderingHelper.NO_MARGINS );

		}
		
		return trh;
	}

}
